package com.market.goods.service.impl;

import com.market.goods.dao.mongo.MarketGoods;
import com.market.goods.dao.mongo.MarketGoodsDetails;
import com.market.goods.dao.mongo.MarketGoodsType;
import com.market.goods.service.MongoDbService;
import com.market.goods.web.vo.QueryMarketGoodsDetailsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MongoDbServiceImpl implements MongoDbService {

    @Autowired
    private MongoTemplate mongoTemplate;

//    @Override
//    public String saveObj(BookTable bookTable) {
////        bookTable.setCreateTime(new Date());
////        bookTable.setUpdateTime(new Date());
//        mongoTemplate.save(bookTable);
//        return "添加成功";
//    }
//
//    @Override
//    public List<BookTable> findAll() {
//        return mongoTemplate.findAll(BookTable.class);
//    }
//
//    /***
//     * 根据id查询
//     * @param bookId
//     * @return
//     */
//    @Override
//    public BookTable getBookById(String bookId) {
//        Query query = new Query(Criteria.where("bookId").is(bookId));
//        return mongoTemplate.findOne(query, BookTable.class);
//    }
//
//    @Override
//    public List<BookTable> getBookByTime(Date startTime, Date endTime) {
//        Query query = new Query(Criteria.where("createTime").gt(startTime).lt(endTime));
//        return mongoTemplate.find(query, BookTable.class);
//    }


    @Override
    public void insertAll(List<Object> objects) {
        mongoTemplate.insertAll(objects);
    }

    @Override
    public void insertMarketGoodsDetailsForMongodb(MarketGoodsDetails marketGoodsDetails) {
        mongoTemplate.save(marketGoodsDetails);
//        return "添加成功";
    }

    @Override
    public MarketGoodsDetails getMarketGoodsById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query, MarketGoodsDetails.class);
    }

    @Override
    public List<MarketGoodsDetails> getMarketGoodsListByShopId(String shopId) {
        Query query = new Query(Criteria.where("shopId").is(shopId));
        return mongoTemplate.find(query, MarketGoodsDetails.class);
    }

    @Override
    public List<MarketGoodsDetails> getPageMarketGoodsList(QueryMarketGoodsDetailsVO queryMarketGoodsDetailsVO) {
        //分页参数
        int page = queryMarketGoodsDetailsVO.getPage();
        if(page <=0){
            page = 1;
        }
        page = page -1;
        int size = queryMarketGoodsDetailsVO.getSize();
        if(size<=0){
            size = 10;
        }
        return null;
    }

    @Override
    public List<MarketGoodsType> getMarketGoodsTypes(MarketGoodsType marketGoodsType) {
        Query query = new Query(Criteria.where("brandId").is(marketGoodsType.getBrandId()));
        return mongoTemplate.find(query, MarketGoodsType.class);
    }


}
